package core.gamemodes;

import java.awt.Color;
import java.awt.Point;
import java.util.Iterator;
import java.util.Vector;

import ai.WorldAI;

import core.*;
import core.actions.AddMachinegun;
import core.actions.FireLaser;
import core.actions.FireMissile;
import core.actions.FireMortar;
import core.actions.FireShotgun;
import core.actions.FireSniper;
import core.actions.MoveHero;
import core.actions.NextUnit;
import core.mountedWeapons.GenericPistol;
import core.units.HeroUnit;
import core.units.Unit;

public class Tutorial extends GameMode{
	
	
	private Vector<Unit> currentTargets=new Vector<Unit>();
	private int phase = 1;
	private boolean messagesOn = true;

	
	public Tutorial(SingleButtonGame m){
		super(m);
		name = "Tutorial";
		forcedMap = "tutorial.txt";
	}
	public void setupPlayerData(PlayerData d){
		Unit u = new HeroUnit(0, 0, null);
		u.setSpriteName("testTankV1.png");
		
		u.actionMap.clear();
		u.actionMap.setWantedDepth(2);
			
		u.actionMap.add(new FireShotgun());
		u.actionMap.add(new MoveHero());	
				
		u.actionMap.add(new FireSniper());
		u.actionMap.add(new FireMissile());

		u.actionMap.set(new FireLaser(), 0, 2);
		
		d.addUnit(u);
		d.selectNextUnit();
		
		
		
	}
	protected void additionalPlayerSetup(Player p){
		p.team=1;
		for(Unit u : p.getPlayerData().getUnits()){
			
			
			//u.linkActions();

		}
	}
	public void initConfiguration(){
		
	}
	public void initGame(){
		
		for(Point p : main.map.getPoints("target"+phase)){
			Unit newUnit = new Unit(CustomUtil.pointOffset(p, 50), AI);
			newUnit.setImageName("target1.png");
			newUnit.setWidth(40000);
			newUnit.setHeight(40000);
			currentTargets.add(newUnit);
			main.units.add(newUnit);
			newUnit.spawn(p);
		}
		phaseMessage();
	}
	public void onFrameLogic(){
		boolean allDead=true;
		Iterator<Unit> it = currentTargets.iterator();
		while(it.hasNext() && allDead){
			if(it.next().isAlive())
				allDead=false;
		}
		if(allDead){
			currentTargets.clear();
			phase++;
			if(main.map.getPoints("target"+phase).isEmpty())
				phase=1;
			for(Point p : main.map.getPoints("target"+phase)){
				Unit newUnit = new Unit(CustomUtil.pointOffset(p, 50), AI);
				newUnit.setImageName("target1.png");
				newUnit.setWidth(40000);
				newUnit.setHeight(40000);
				currentTargets.add(newUnit);
				main.units.add(newUnit);
				newUnit.spawn(p);
			}
			phaseMessage();
		}
	}
	private void phaseMessage(){
		MessageHandler.clearMessages();
		switch(phase){
		case 1:
			MessageHandler.showMessage("At the top of the screen you see a spinning menu. Use it to select the Shotgun by pressing your button when the menu is pointing at the Shotgun icon. After that, just aim and fire!", "iconShotgun.png");
			break;
		case 2:
			MessageHandler.showMessage("For targets out of reach, use the Sniper.", "iconSniper.png");
			break;
		case 3:
			MessageHandler.showMessage("Walls are blocking these targets. Use the Move command to get in line of sight.", "iconMove3.png");
			MessageHandler.addMessage("To select an inner action, wait until your menu is pointing in the right direction, and then quickly doubleclick", "iconMove3.png");

			break;
		case 4:
			MessageHandler.showMessage("Congratulations! You learned how to command your unit using only a single button!", "iconInfo.png");
			break;
		default:
			break;
			
		}
	}
	public void setupAI(){
		PlayerData pd = new PlayerData('g');
		
		Unit u = new HeroUnit(0, 0, null);
		pd.addUnit(u);
		pd.selectNextUnit();
		u.actionMap.clear();
		u.actionMap.setWantedDepth(1);		
		u.actionMap.add(new FireShotgun());
		u.actionMap.add(new MoveHero());
		
		pd.setPlayerColor(Color.white);
		Point spawnPoint = new Point(100000, 100000);
		AI = new WorldAI(135*playerCounter+80, 100, playerCounter, pd.getKey(), spawnPoint, main);
		AI.setPlayerData(pd);
		AI.team=2;
		/*AI.toMainSpinner();
		AI.spawnFirstUnit();
		main.players.add(AI);
		*/
	}

}
